package com.jarvis.hospital.prescription;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jarvis.hospital.R;
import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.common.ResponseData;
import com.jarvis.hospital.prescription.adapter.DefaultPrescriptionAdapter;
import com.jarvis.hospital.prescription.model.PrescriptionItemModel;
import com.jarvis.hospital.prescription.model.PrescriptionModel;
import com.jarvis.hospital.servers.ApiService;
import com.jarvis.hospital.util.DividerItemDecoration;
import com.jarvis.hospital.util.net.RequestApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DefaultPrescriptionListActivity extends BaseActivity {
    @BindView(R.id.recy_prescription)
    RecyclerView recy_prescription;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.tv_title)
    TextView tv_title;
    private DefaultPrescriptionAdapter prescriptionAdapter;
    private ApiService service;

    @Override
    public int getLayoutId() {
        return R.layout.activity_default_prescription_list;
    }

    @Override
    public void initData() {
        showDialog("加载中");
        service.getDefaultProgrammeList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData<PrescriptionModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData<PrescriptionModel> prescriptionModelResponseData) {
                        if (prescriptionModelResponseData != null) {
                            int resultCode = prescriptionModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                List<PrescriptionItemModel> prescriptionList = prescriptionModelResponseData.getData().getPrescriptionList();
                                PrescriptionItemModel model = new PrescriptionItemModel();
                                model.setProgrammeName("新建方案");
                                model.setProgrammeId("-1");
                                prescriptionList.add(model);
                                prescriptionAdapter.addData(prescriptionList);
                            } else {
                                showTextToast(prescriptionModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }

    @Override
    public void initView() {
        service = RequestApi.create(ApiService.class);
        //setStatus(true);
        tv_title.setText("健康处方");
        rel_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAdapter();
        recy_prescription.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                PrescriptionItemModel prescriptionItemModel = prescriptionAdapter.getData().get(i);
                Bundle bundle = new Bundle();
                bundle.putString("programmeId", prescriptionItemModel.getProgrammeId());
                bundle.putInt("type",1);
                startActivity(NewPrescriptionActivity.class, bundle);
            }
        });
    }

    /**
     * 设置adapter
     */
    public void setAdapter() {
        prescriptionAdapter = new DefaultPrescriptionAdapter(R.layout.item_prescription, new ArrayList<PrescriptionItemModel>());
        recy_prescription.setLayoutManager(new LinearLayoutManager(this));
        recy_prescription.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, getResources().getDrawable(R.drawable.divier_order_gray)));
        prescriptionAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        recy_prescription.setAdapter(prescriptionAdapter);
    }
}
